# Flores, Alejandro and Alexander Coppock
# Do Bilinguals Respond More Favorably to Candidate Advertisements in English or in Spanish?
# Forthcoming, Political Communication

rm(list = ls())

# Set your working directory to the replication archive directory

# Install software if necessary
# install.packages(c("stargazer", "tidyverse", "estimatr"))

library(stargazer)
library(tidyverse)
library(estimatr)

study_1 <- read_csv("study_1.csv")
study_2 <- read_csv("study_2.csv")
study_3 <- read_csv("study_3.csv")

study_1 <- study_1 %>% mutate(Z_ad = as.numeric(Z_ad == "spanish_ad"))


# Table A.1 ---------------------------------------------------------------

fit_0_b <- lm(bush_general ~ Z_ad*Z_survey, data = filter(study_1, bilingual == 1))
fit_1_b <- lm(vela_general ~ Z_ad*Z_survey, data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_vela)))
fit_2_b <- lm(coffman_general ~ Z_ad*Z_survey, data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_coffman)))

stargazer(fit_0_b, fit_1_b, fit_2_b,
          p = starprep(fit_0_b, fit_1_b, fit_2_b, stat = "p"),
          se = starprep(fit_0_b, fit_1_b, fit_2_b, stat = "std.error"),
          covariate.labels = c("Spanish-language Ad", "Spanish-language Survey", "Ad X Survey",  "Constant (Control Mean)"),
          dep.var.labels = c("Bush: General Election", "Vela: General Election", "Coffman: General Election"),
          add.lines = list(c("Sample", "Bilingual", "Bilingual", "Bilingual")),
          style = "apsr",
          omit.stat = c("f", "ser", "adj.rsq", "rsq"),
          title = "Effects of Treatments on General Election Support",
          label = "general_outcomes_int",
          column.sep.width = "0pt",
          font.size = "footnotesize",
          notes = c("HC2 robust standard errors are in parentheses."))

# Table A.2 ---------------------------------------------------------------

fit_6_b <- lm(bush_cares ~ Z_ad*Z_survey, data = filter(study_1, bilingual == 1))
fit_7_b <- lm(vela_cares ~ Z_ad*Z_survey, data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_vela)))
fit_8_b <- lm(coffman_cares ~ Z_ad*Z_survey, data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_coffman)))

stargazer(fit_6_b, fit_7_b, fit_8_b,
          p = starprep(fit_6_b, fit_7_b, fit_8_b, stat = "p"),
          se = starprep(fit_6_b, fit_7_b, fit_8_b),
          covariate.labels = c("Spanish-language Ad", "Spanish-language Survey", "Ad X Survey",  "Constant (Control Mean)"),
          dep.var.labels = c("Bush Cares", "Vela Cares", "Coffman Cares"),
          add.lines = list(c("Sample", "Bilingual", "Bilingual", "Bilingual")),
          style = "apsr",
          omit.stat = c("f", "ser", "adj.rsq", "rsq"),
          title = "Effects of Treatments on Perceptions of Candidate Caring",
          label = "care_outcomes_int",
          column.sep.width = "0pt",
          font.size = "footnotesize",
          notes = c("HC2 robust standard errors are in parentheses."))



# Table A.3 ---------------------------------------------------------------

fit_9_b <- lm(conf_in_bush ~ Z_ad*Z_survey, data = filter(study_1, bilingual == 1))
fit_10_b <- lm(conf_in_vela ~ Z_ad*Z_survey, data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_vela)))
fit_11_b <- lm(conf_in_coffman ~ Z_ad*Z_survey, data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_coffman)))

stargazer(fit_9_b, fit_10_b, fit_11_b,
          p = starprep(fit_9_b, fit_10_b, fit_11_b, stat = "p"),
          se = starprep(fit_9_b, fit_10_b, fit_11_b),
          covariate.labels = c("Spanish-language Ad", "Spanish-language Survey", "Ad X Survey",  "Constant (Control Mean)"),
          dep.var.labels = c("Confidence in Bush", "Confidence in Vela", "Confidence in Coffman"),
          add.lines = list(c("Sample", "Bilingual", "Bilingual", "Bilingual")),
          style = "apsr",
          omit.stat = c("f", "ser", "adj.rsq", "rsq"),
          title = "Effects of Treatments on \n Confidence in Candidate to do the Right Thing on Immigration",
          label = "confidence_int_outcomes",
          column.sep.width = "0pt",
          font.size = "footnotesize",
          notes = c("HC2 robust standard errors are in parentheses."))


# Table A.4 ---------------------------------------------------------------

fit_3_b <- lm(like_bush ~ Z_ad*Z_survey, data = filter(study_1, bilingual == 1))
fit_4_b <- lm(like_vela ~ Z_ad*Z_survey, data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_vela)))
fit_5_b <- lm(like_coffman ~ Z_ad*Z_survey, data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_coffman)))

stargazer(fit_3_b, fit_4_b, fit_5_b,
          p = starprep(fit_3_b, fit_4_b, fit_5_b, stat = "p"),
          se = starprep(fit_3_b, fit_4_b, fit_5_b),
          covariate.labels = c("Spanish-language Ad", "Spanish-language Survey", "Ad X Survey",  "Constant (Control Mean)"),
          dep.var.labels = c("Like Bush", "Like Vela", "Like Coffman"),
          add.lines = list(c("Sample", "Bilingual", "Bilingual", "Bilingual")),
          style = "apsr",
          omit.stat = c("f", "ser", "adj.rsq", "rsq"),
          title = "Effects of Treatments on Liking Candidate (1-7)",
          label = "like_outcomes_int",
          column.sep.width = "0pt",
          font.size = "footnotesize",
          notes = c("HC2 robust standard errors are in parentheses."))
